package com.yuzhe.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.yuzhe.model.News;
import com.yuzhe.model.NewsType;
import com.yuzhe.utils.HtmlUtils;
import com.yuzhe.utils.JSONResult;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;

import java.util.Date;
import java.util.List;

import static com.yuzhe.common.Constant.BASE_SERVER_URL;

public class NewsController extends Controller {

    public void getNewsList() {
        String pageNumberStr = getPara("pageNumber");
        int pageNum = 0;
        if (StringUtils.isBlank(pageNumberStr)){
            pageNum = 1;
        } else if (!StringUtils.isBlank(pageNumberStr)){
            pageNum = Integer.parseInt(pageNumberStr);
            if (pageNum<=0){
                pageNum = 1;
            }
        }

        Page<News> paginate = News.dao.paginate(pageNum, 10, "select news.*,news_type.typename", "from news left join news_type on news.news_type_id = news_type.id  order by create_time desc");
        renderJson(JSONResult.ok(paginate));

    }

    public void getNewsById(){
        String id = getPara("id");
        String sql = "select title,content,create_time from news where id = ?";
        News news = News.dao.findFirst(sql, id);
        if (news!=null){
            String content = news.getContent();
            String s = HtmlUtils.addServerUrlImgUrl(content, BASE_SERVER_URL);
            news.setContent(s);
            renderJson(JSONResult.ok(news));
        } else {
            renderJson(JSONResult.errorMsg("没有查到对应数据"));
        }
    }

    //删除新闻
    public void deleteNews() {
        String ids = getPara("ids");
        if (StringUtils.isBlank(ids)) {
            renderJson(JSONResult.errorMsg("新闻id不能为空"));
            return;
        }

        String sql = "delete from news where id in (" + ids + ")";
        int update = Db.update(sql);
        if (update > 0) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("批量删除失败"));
        }
    }

    public void saveNews() {
        String title = getPara("title");
        String type = getPara("type");
        String sort = getPara("sort");
        String keywords = getPara("keywords");
        String content = getPara("content");
        String imgUrl = getPara("fileList");

        if (StringUtils.isBlank(title)){
            renderJson(JSONResult.errorMsg("标题不能为空"));
            return;
        }

        if (StringUtils.isBlank(type)){
            renderJson(JSONResult.errorMsg("分类不能为空"));
            return;
        }

        if (StringUtils.isBlank(content)){
            renderJson(JSONResult.errorMsg("内容不能为空"));
            return;
        }

        content = HtmlUtils.removeServerUrlImgUrl(content,BASE_SERVER_URL);

        if (StringUtils.isBlank(sort)){
            sort = "50";
        }

        String id = Sid.next();
        boolean save = News.dao.setId(id).setTitle(title).setNewsTypeId(type)
                .setSort(Integer.valueOf(sort)).setKeyword(keywords)
                .setImgurl(imgUrl).setContent(content)
                .setCreateTime(new Date()).save();
        if (save){
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("保存失败"));
        }

    }


    public void updateNews() {
        String id = getPara("id");
        String title = getPara("title");
        String type = getPara("type");
        String sort = getPara("sort");
        String keywords = getPara("keywords");
        String content = getPara("content");
        String imgUrl = getPara("fileList");

        if (StringUtils.isBlank(id)){
            renderJson(JSONResult.errorMsg("ID不能为空"));
            return;
        }

        if (StringUtils.isBlank(title)){
            renderJson(JSONResult.errorMsg("标题不能为空"));
            return;
        }

        if (StringUtils.isBlank(type)){
            renderJson(JSONResult.errorMsg("分类不能为空"));
            return;
        }

        if (StringUtils.isBlank(content)){
            renderJson(JSONResult.errorMsg("内容不能为空"));
            return;
        }

        if (StringUtils.isBlank(sort)){
            sort = "50";
        }

        boolean save = News.dao.setId(id).setTitle(title).setNewsTypeId(type)
                .setSort(Integer.valueOf(sort)).setKeyword(keywords)
                .setImgurl(imgUrl).setContent(content).update();
        if (save){
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("保存失败"));
        }

    }



    public void saveNewsType() {
        String id = Sid.next();
        String typeName = getPara("typeName");
        String sort = getPara("sort");
        if (StringUtils.isBlank(typeName)) {
            renderJson(JSONResult.errorMsg("分类名称不能为空"));
            return;
        }

        if (StringUtils.isBlank(sort)) {
            renderJson(JSONResult.errorMsg("分类排序不能为空"));
            return;
        }

        boolean result = NewsType.dao.set("id", id).set("typename", typeName)
                .set("sort", sort).save();
        if (result) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("添加失败"));
        }
    }

    public void updateNewsType() {
        String id = getPara("id");
        if (StringUtils.isBlank(id)) {
            renderJson(JSONResult.errorMsg("分类id不能为空"));
            return;
        }

        String typeName = getPara("typeName");
        String sort = getPara("sort");
        if (StringUtils.isBlank(typeName)) {
            renderJson(JSONResult.errorMsg("分类名称不能为空"));
            return;
        }

        if (StringUtils.isBlank(sort)) {
            renderJson(JSONResult.errorMsg("分类排序不能为空"));
            return;
        }

        boolean result = NewsType.dao.set("id", id).set("typename", typeName)
                .set("sort", sort).update();
        if (result) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("更新失败"));
        }
    }

    //获取所有分类列表
    public void getNewsTypeList() {
        String sql = "select * from news_type order by sort desc";
        List<NewsType> newsTypes = NewsType.dao.find(sql);
        renderJson(JSONResult.ok(newsTypes));
    }

    //删除分类
    public void deleteNewsType() {
        String ids = getPara("ids");
        if (StringUtils.isBlank(ids)) {
            renderJson(JSONResult.errorMsg("分类id不能为空"));
            return;
        }

//        String[] idsArray = ids.split(",");
        String sql = "delete from news_type where id in (" + ids + ")";
        int update = Db.update(sql);
        if (update > 0) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("批量删除失败"));
        }
    }
}
